package com.alex.algorithm.easy;

/**
 * 投票算法
 */
public class MajorityElement {
    public static void main(String[] args) {
        System.out.println(majority(new int[]{3,2,3}));
        System.out.println(majority(new int[]{2,2,1,1,1,2,2}));
    }

    public static int majority(int[] intArray) {
        int count = 1;
        int majority = intArray[0];
        for (int i = 1; i < intArray.length; ++i) {
            if (count == 0 ) {
                majority = intArray[i];
            }else if (majority == intArray[i]) {
                ++count;
            }else {
                --count;
            }
        }
        return majority;
    }
}
